package com.zdb.java.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo {

    public static void main(String[] args) {

//        testExecutors();
        testRunnable();
    }

    private static void testRunnable() {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "\t" + "-----");
            }
        };
        for (int i = 0; i < 10; i++) {
            new Thread(r, "t_" + i).start();
        }
    }

    private static void testExecutors() {
        ExecutorService threadPool = null;
        threadPool = Executors.newCachedThreadPool();
        threadPool = Executors.newSingleThreadExecutor();
        threadPool = Executors.newFixedThreadPool(3);
        threadPool = Executors.newWorkStealingPool();


        for (int i = 0; i < 20; i++) {
            threadPool.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "\t" + "-------------");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

        }
    }
}
